<?php

function RndCode() {
	$strSymbols="1234567890";
	$strRet = "";
	$i = 0;

	mt_srand((double)microtime() * 1000000);

	while($i<4) 
	{
		$strRet.=$strSymbols[mt_rand(0, strlen($strSymbols)-1)];
		$i++;
	}
	return $strRet;
}

session_start();

// specify the text to display
if(isset($_SESSION["SecurityCode"]) && $_SESSION["SecurityCode"] != "") {
	$textstr = $_SESSION["SecurityCode"];
}
else {
	$textstr = RndCode();
	$_SESSION["SecurityCode"] = $textstr;
}

// create a 44*14 image
$img = ImageCreate(44,14) or die("Cannot create image");

// create white background and black text
$bgcolor = ImageColorAllocate($img,255,255,255);
$textcolor = ImageColorAllocate($img,0,0,0);

// set the background color as transparent
ImageColorTransparent($img, $bgcolor);

// fill the background
ImageFill($img,0,0,$bgcolor);

// write the string at the top left
ImageString($img,5,6,1,$textstr,$textcolor);

// output the image
header("Content-type: image/png");
ImagePng($img);

// destroy the image to free out memory
ImageDestroy($img);

?>
